//
//  MyDrawerViewController.m
//  A_DLS151129
//
//  Created by 151129-49宋强 on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "MyDrawerViewController.h"
#import "GoodViewController.h"
#import "SetViewController.h"
#import "Player.h"

#define Width self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface MyDrawerViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)Model *model;
@end

@implementation MyDrawerViewController
- (void)dealloc
{   [_model release];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"播放" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"音乐" object:nil];
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    [self creatUserView];//创建用户信息View
    [self creatFourButton];//用户信息下面的四个button
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 220, self.view.frame.size.width, self.view.frame.size.height - 100) style:UITableViewStylePlain];
    self.automaticallyAdjustsScrollViewInsets = NO;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
#pragma mark - 创建音乐界面
    [self creatMusicView];
#pragma mark - 注册通知中心
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(centerAction:) name:@"音乐" object:nil];
    [center addObserver:self selector:@selector(setBtn:) name:@"播放" object:nil];
  }

#pragma mark - 监听是否播放设置btn背景图片
- (void)setBtn:(NSNotification *)noti
{
    UIButton *btn = [self.view viewWithTag:5000];
    if ([noti.object isEqualToString:@"开始"]) {
        [btn setImage:[UIImage imageNamed:@"kaishi.png"] forState:UIControlStateNormal];
   }else{
        [btn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
   }
   }
- (void)centerAction:(NSNotification* )noti
{
    self.model = noti.object;
   UIImageView *imgView = [self.view viewWithTag:2000];
    [imgView sd_setImageWithURL:[NSURL URLWithString:_model.coverimg]];
    UILabel *titleLabel = [self.view viewWithTag:3000];
    titleLabel.text = _model.title;
    
}
#pragma mark - 创建音乐界面方法
- (void)creatMusicView
{
    CGFloat width = Width - 60;
    UIImageView *backView = [[UIImageView alloc] initWithFrame:CGRectMake(0, HEIGHT - 60, width, 60)];
    backView.userInteractionEnabled = YES;
    backView.image = [UIImage imageNamed:@"bg.png"];
    [self.view addSubview:backView];
    [backView release];
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    imgView.image = [UIImage imageNamed:@"yinyue.png"];
    imgView.tag = 2000;
    //imgView.backgroundColor = [UIColor redColor];
    [backView addSubview:imgView];
    [imgView release];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 10, 160, 40)];
    titleLabel.tag = 3000;
    titleLabel.text = @"当前没有电台";
   // titleLabel.backgroundColor = [UIColor redColor];
    [backView addSubview:titleLabel];
    [titleLabel release];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
   // btn.backgroundColor = [UIColor redColor];
    btn.frame = CGRectMake(width - 50 , 10, 40, 40);
    [btn setImage:[UIImage imageNamed:@"kaishi.png"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(playerAction:) forControlEvents:UIControlEventTouchUpInside];
     btn.tag = 5000;
    [backView addSubview:btn];
}
#pragma mark - 播放 暂停按钮
- (void)playerAction:(UIButton *)btn
{
    BOOL isplay = [[NSUserDefaults standardUserDefaults] boolForKey:@"isPlay"];
    if (isplay) {
        [btn setImage:[UIImage imageNamed:@"zanting.png"] forState:UIControlStateNormal];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"是否播放"object:nil userInfo:[NSMutableDictionary dictionaryWithObject:@"0" forKey:@"playInfo"]];
    }else{
        [btn setImage:[UIImage imageNamed:@"kaishi.png"] forState:UIControlStateNormal];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"是否播放"object:nil userInfo:[NSMutableDictionary dictionaryWithObject:@"1" forKey:@"playInfo"]];
    }
   // btn.selected = !btn.selected;
}
#pragma mark - 创建用户View
- (void)creatUserView
{
    UIImageView *headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 50, 60, 60)];
    headImgView.layer.cornerRadius = 30;
    headImgView.image = [UIImage imageNamed:@"user.png"];
    headImgView.tag = 666;
    headImgView.userInteractionEnabled = YES;
    [self.view addSubview:headImgView];
    [headImgView release];
    UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(headImgView.frame.size.width + 20 + 5, headImgView.frame.origin.y, Width - 85, 60)];
    userLabel.tag = 777;
    userLabel.text = @"登陆/注册";
    userLabel.userInteractionEnabled = YES;
    [self.view addSubview:userLabel];
    [userLabel release];
    //创建手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [headImgView addGestureRecognizer:tap];
    [tap release];
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [headImgView addGestureRecognizer:tapRight];
    [userLabel addGestureRecognizer:tapRight];
    [tapRight release];
    //显示本地内容 头像 姓名 等
    //获取本地数据
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userInfo.plist"];
    NSDictionary *userDic = [NSDictionary dictionaryWithContentsOfFile:path];
    //如果存在则显示
    if (userDic && ([userDic[@"uName"] length] > 0)) {
        [headImgView sd_setImageWithURL:[NSURL URLWithString:userDic[@"iconUrl"]]];
        userLabel.text = userDic[@"uName"];
    }
 }
#pragma mark - 登陆/注册
- (void)tapAction:(UITapGestureRecognizer *)tap
{
    BOOL isLogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"];
    if (!isLogin) {
        [self loginAndSetUserInfo];//登录并设置用户信息
    }else{
        //判断是否切换用户
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否切换用户" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self loginAndSetUserInfo];//切换用户
        }];
        UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:yesAction];
        [alert addAction:noAction];
        [self presentViewController:alert animated:yesAction completion:^{
            
        }];
    }
    
}
#pragma mark -友盟三方登录加设置用户信息
- (void)loginAndSetUserInfo{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        if (response.responseCode == UMSResponseCodeSuccess) {
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            //改变头像
            UIImageView *headView = [self.view viewWithTag:666];
            [headView sd_setImageWithURL:[NSURL URLWithString:snsAccount.iconURL]];
            //显示名字
            UILabel *nameLabel = [self.view viewWithTag:777];
            nameLabel.text = snsAccount.userName;
            //登录状态为YES
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isLogin"];
            //存本地
            NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userInfo.plist"];
            NSDictionary *dic = @{@"uName": snsAccount.userName, @"iconUrl": snsAccount.iconURL};
            [dic writeToFile:path atomically:YES];
        }});

}
#pragma mark - 创建四个button
- (void)creatFourButton
{
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 1000 + i;
        if (i == 0) {
            [btn setImage:[UIImage imageNamed:@"xiazai.png"] forState:UIControlStateNormal];
        }else if (i == 1){
            [btn setImage:[UIImage imageNamed:@"heixin.png"] forState:UIControlStateNormal];
        }else if (i == 2){
            [btn setImage:[UIImage imageNamed:@"xiaoxi.png"] forState:UIControlStateNormal];
        }else{
            [btn setImage:[UIImage imageNamed:@"xie.png"] forState:UIControlStateNormal];
        }
        btn.frame = CGRectMake(20 + i * 60, 140, 30, 30);
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];

    }
    
}
#pragma mark - button点击方法
- (void)btnAction:(UIButton *)btn
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    if (btn.tag == 1000) {
        NSLog(@"下载");
    }else if (btn.tag == 1001){
        //关闭抽屉
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
            
        }];
        CollectionViewController *collectVC = [[CollectionViewController alloc] init];
        [app.navi pushViewController:collectVC animated:YES];
        [collectVC release];
    }else if (btn.tag == 1002){
        NSLog(@"消息");
    }else{
        NSLog(@"说说");
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *app = [[UIApplication sharedApplication] delegate];
    if (indexPath.row == 0) {
        //关闭抽屉
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        //跳转页面
        [app.navi popToRootViewControllerAnimated:YES];
    }else if (indexPath.row == 1){
        //关闭抽屉
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        //跳转页面
        RadioViewController *radioVC = [[RadioViewController alloc] init];
        [app.navi pushViewController:radioVC animated:YES];
        [radioVC release];
        
    }else if (indexPath.row == 2) {
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        ReadViewController *readVC = [[ReadViewController alloc] init];
        [app.navi pushViewController:readVC animated:YES];
        [readVC release];
    }else if (indexPath.row == 4){
            [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
            }];
            GoodViewController *goodVC = [[GoodViewController alloc] init];
            [app.navi pushViewController:goodVC animated:YES];
            [goodVC release];
    }else if (indexPath.row ==5){
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        SetViewController *setVC = [[SetViewController alloc] init];
        [app.navi pushViewController:setVC animated:YES];
        [setVC release];
    }else if (indexPath.row == 3){
        [app.mmDrawer closeDrawerAnimated:YES completion:^(BOOL finished) {
        }];
        FragmentViewController *fragVC = [[FragmentViewController alloc] init];
        [app.navi pushViewController:fragVC animated:YES];
        [fragVC release];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.imageView.image = [UIImage imageNamed:@"shouye1.png"];
        cell.textLabel.text = @"首页";
    }else if (indexPath.row == 1){
        cell.imageView.image = [UIImage imageNamed:@"shouyinji.png"];
        cell.textLabel.text = @"电台";
    }else if (indexPath.row == 2){
        cell.imageView.image = [UIImage imageNamed:@"yuedu.png"];
        cell.textLabel.text = @"阅读";
    }else if (indexPath.row == 3){
        cell.imageView.image = [UIImage imageNamed:@"suipian.png"];
        cell.textLabel.text = @"碎片";
    }else if (indexPath.row == 4){
        cell.imageView.image = [UIImage imageNamed:@"coffe.png"];
        cell.textLabel.text = @"良品";
    }else {
        cell.imageView.image = [UIImage imageNamed:@"set.png"];
        cell.textLabel.text = @"设置";
    }
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
